<?php

// 本类由系统自动生成，仅供测试用途

namespace Home\Controller ;

use Think\Controller ;

class IndexController extends \Think\Controller
{

    protected $UserLoginModel            = null ;
    protected $StudentBaseModel          = null ;
    protected $ActivityDictionaryModel   = null ;
    protected $ActivityLinkModel         = null ;
    protected $AcademyDictionaryModel    = null ;
    protected $ProfessionDictionaryModel = null ;

    function __construct ()
    {
        parent::__construct () ;
        $this->UserLoginModel            = new \Home\Model\UserLoginModel() ;
        $this->StudentBaseModel          = new \Home\Model\StudentBaseModel() ;
        $this->ActivityDictionaryModel   = new \Home\Model\ActivityDictionaryModel() ;
        $this->ActivityLinkModel         = new \Home\Model\UserActivityLinkModel() ;
        $this->AcademyDictionaryModel    = new \Home\Model\AcademyDictionaryModel() ;
        $this->ProfessionDictionaryModel = new \Home\Model\ProfessionDictionaryModel() ;
    }

    public function index ()
    {
        //$this->redirect ( 'updateStudentBaseInfor' ) ;
    }

//用户登录页面
    function login ()
    {
        $this->display () ;
    }

//修改学生基本信息
    function updateStudentBaseInfor ()
    {
        $UID           = session ( 'UID' ) ;
        $UName         = I ( 'UName' , null ) ;
        $UIDCardNumber = I ( 'UIDCardNumber' , null ) ;
        //通过身份证号码获取用户出生日期
        $UBirthday     = substr ( $UIDCardNumber , 6 , 8 ) ;
        //性别通过身份证获取 奇数为男 偶数为女
        $USex          = substr ( $UIDCardNumber , 16 , 1 ) % 2 ;
        $UEmail        = I ( 'UEmail' , null ) ;
        $UPhoneNumber  = I ( 'UPhoneNumber' , null ) ;
        $UQQ           = I ( 'UQQ' , null ) ;
        $UPicturePath  = I ( 'UPicturePath' , null ) ;
        $AcademyID     = I ( 'AcademyID' , null ) ;
        $ProfessionID  = I ( 'ProfessionID' , null ) ;
        $GradeID       = I ( 'GradeID' , null ) ;
        $ClassID       = I ( 'ClassID' , null ) ;

        //验证身份证号码是否有效
        if ( ! $this->StudentBaseModel->checkUIDCardnumber ( $UIDCardNumber ) )
        {
            $this->error ( '身份证号码无效,请重新输入' ) ;
        }

        //验证邮箱格式
        if ( ! $this->StudentBaseModel->checkEmailFormat ( $UEmail ) )
        {
            $this->error ( '邮箱格式错误！' ) ;
        }

        //验证手机格式
        if ( ! $this->StudentBaseModel->checkTelFormat ( $UPhoneNumber ) )
        {
            $this->error ( '手机格式错误！' ) ;
        }

        //修改学生基本信息
        if ( $this->StudentBaseModel->updateStudentBaseInfor ( $UID , $UName ,
                        $USex , $UIDCardNumber , $UEmail , $UPhoneNumber ,
                        $UQQ , $UPicturePath , $AcademyID , $ProfessionID ,
                        $GradeID , $ClassID , $UBirthday ) )
        {
            $this->success ( '修改学生基本信息成功' ) ;
        }
        else
        {
            $this->error ( '修改失败' ) ;
        }
    }

//核对用户密码
    function checkUPassword ()
    {
        $UID      = I ( 'UID' , null ) ;
        $Password = I ( 'Password' , null ) ;
        $rs       = $this->UserLoginModel->checkUPassword ( $UID , $Password ) ;
        $Status   = $this->UserLoginModel->getUserStatusByUID ( $UID ) ;
        if ( $rs )
        {
            session ( 'UID' , $UID ) ;
            switch ( $Status )
            {
                case 0:
                    $this->redirect ( 'studentInfor' ) ;
                    break ;
                case 1:
                    $this->redirect ( 'admin' ) ; //对字典表操作
                    break ;
                default :
                    $this->error ( "未知操作属性" ) ;
                    break ;
            }
        }
        else
        {
            $this->error ( "用户名或密码错误" , U ( 'login' ) ) ;
        }
    }

    function addActivityInfor ()
    {
        $ActivityName               = I ( 'ActivityName' , null ) ;
        $ActivityOrganization       = I ( 'ActivityOrganization' , null ) ;
        $ActivityTimeStart          = I ( 'ActivityTimeStart' , null ) ;
        $ActivityTimeEnd            = I ( 'ActivityTimeEnd' , null ) ;
        $ActivityHours              = I ( 'ActivityHours' , null ) ;
        $ResponsiblePerson          = I ( 'ResponsiblePersonName' , null ) ;
        $ResponsiblePersonTelephone = I ( 'ResponsiblePersonTelephone' , null ) ;
        $ActivityResult             = I ( 'ActivityResult' , null ) ;
        $rs                         = $this->ActivityDictionaryModel->addActivityInfor ( $ActivityName ,
                $ActivityOrganization , $ActivityTimeStart , $ActivityTimeEnd ,
                $ActivityHours , $ResponsiblePerson ,
                $ResponsiblePersonTelephone , $ActivityResult ) ;
        if ( $rs )
        {
            $this->success ( "新增活动成功！" ) ;
        }
        else
        {
            $this->error ( "新增活动失败！" ) ;
        }
    }

//添加学生参加活动记录
    function addUserActivityLink ()
    {
        $UID        = I ( 'UID' , null ) ;
        $ActivityID = I ( 'ActivityID' , null ) ;
        $rs         = $this->ActivityLinkModel->addUserActivityLink ( $UID ,
                $ActivityID ) ;
        if ( $rs )
        {
            $this->success ( "添加学生活动记录成功" ) ;
        }
        else
        {
            $this->error ( "添加记录失败" ) ;
        }
    }

//录入学生基本信息操作
    function addStudentBaseInformation ()
    {
        $UID           = I ( 'UID' , null ) ;
        $UName         = I ( 'UName' , null ) ;
        $UBirthday     = I ( 'UBirthday' , null ) ;
        $UIDCardNumber = I ( 'UIDCardNumber' , null ) ;
        $UEmail        = I ( 'UEmail' , null ) ;
        $UPhoneNumber  = I ( 'UPhoneNumber' , null ) ;
        $UQQ           = I ( 'UQQ' , null ) ;
        $UPicturePath  = I ( 'UPicturePath' , null ) ;
        $AcademyID     = I ( 'AcademyID' , null ) ;
        $ProfessionID  = I ( 'ProfessionID' , null ) ;
        $GradeID       = I ( 'GradeID' , null ) ;
        $ClassID       = I ( 'ClassID' , null ) ;
        $rs            = $this->StudentBaseModel->addStudentBaseInfor ( $UID ,
                $UName , $UBirthday , $UIDCardNumber , $UEmail , $UPhoneNumber ,
                $UQQ , $UPicturePath , $AcademyID , $ProfessionID , $GradeID ,
                $ClassID ) ;
        if ( $rs )
        {
            $this->success ( "录入学生基本信息成功" , U ( 'admin' ) ) ;
        }
        else
        {
            $this->error ( "录入学生基本信息失败" ) ;
        }
    }

    /**
     * 通过UID获取参加活动情况
     */
    function getActivityInforByUID ()
    {
        $UID  = session ( 'UID' ) ;
        $Year = I ( 'Year' , null ) ;
//获取ActivityID数组
        $Link = $this->ActivityLinkModel->getActivityIDByUID ( $UID ) ;
        foreach ( $Link as
                $key =>
                $value )
        {
            $ActivityID[] = $value[ 'ActivityID' ] ;
        }
        $rs = $this->ActivityDictionaryModel->getActivityInforByActivityID ( $ActivityID ,
                $Year ) ;

        return $rs ? $rs : false ;
    }

    /**
     * 用户修改密码
     * @param type $UID
     * @param type $NewPassword
     * @return boolean
     */
    function updateUPassword ()
    {
        $UID         = session ( 'UID' ) ;
        $Password    = I ( 'Password' , null ) ;
        $NewPassword = I ( 'NewPassword' , null ) ;
        $RePassword  = I ( 'Repassword' , null ) ;
        //验证密码是否正确
        if ( $this->UserLoginModel->checkUPassword ( $UID , $Password ) )
        {
            //验证两次输入的密码是否一致
            if ( $NewPassword === $RePassword )
            {
                if ( $this->UserLoginModel->updateUPassword ( $UID ,
                                $NewPassword ) )
                {
                    $this->success ( '密码修改成功！' ) ;
                }
                else
                {
                    $this->error ( '密码修改失败！' ) ;
                }
            }
            $this->error ( '密码输入不一致，请重新输入密码！' ) ;
        }
        else
        {
            $this->error ( '密码错误！' ) ;
        }
    }

}
